using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Hematology.Data
{
	[DataObject]
	[Serializable]
	public partial class FileAttach
	{
		#region Constants

		private const string TableName = "[dbo].[FileAttach]";

		#endregion

		#region Properties

		public int? AttachId { get; set; }
		public int? TemplateId { get; set; }
		public int? SiteContentId { get; set; }
		public int? EventId { get; set; }
		public string DocumentName { get; set; }
		public string DocumentType { get; set; }
		public int? DocumentSize { get; set; }
		public bool? IsDeleted { get; set; }
		public DateTime? CreateDate { get; set; }
		public DateTime? LastModifiedDate { get; set; }

		#endregion

		#region Methods

		#region Mapping Methods

		protected void MapTo(DataSet ds)
		{
			if (ds == null)
				ds = new DataSet();

			if (ds.Tables[TableName] == null)
				ds.Tables.Add(TableName);

			ds.Tables[TableName].Columns.Add("AttachId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("TemplateId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("SiteContentId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("EventId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("DocumentName", typeof(String));
			ds.Tables[TableName].Columns.Add("DocumentType", typeof(String));
			ds.Tables[TableName].Columns.Add("DocumentSize", typeof(Int32));
			ds.Tables[TableName].Columns.Add("IsDeleted", typeof(Boolean));
			ds.Tables[TableName].Columns.Add("CreateDate", typeof(DateTime));
			ds.Tables[TableName].Columns.Add("LastModifiedDate", typeof(DateTime));

			DataRow dr = ds.Tables[TableName].NewRow();

			dr["AttachId"] = AttachId ?? (object) DBNull.Value;
			dr["TemplateId"] = TemplateId ?? (object) DBNull.Value;
			dr["SiteContentId"] = SiteContentId ?? (object) DBNull.Value;
			dr["EventId"] = EventId ?? (object) DBNull.Value;
			dr["DocumentName"] = DocumentName ?? (object) DBNull.Value;
			dr["DocumentType"] = DocumentType ?? (object) DBNull.Value;
			dr["DocumentSize"] = DocumentSize ?? (object) DBNull.Value;
			dr["IsDeleted"] = IsDeleted ?? (object) DBNull.Value;
			dr["CreateDate"] = CreateDate ?? (object) DBNull.Value;
			dr["LastModifiedDate"] = LastModifiedDate ?? (object) DBNull.Value;

			ds.Tables[TableName].Rows.Add(dr);
		}

		protected void MapFrom(DataRow dr)
		{
			AttachId = dr["AttachId"] != DBNull.Value ? Convert.ToInt32(dr["AttachId"]) : AttachId = null;
			TemplateId = dr["TemplateId"] != DBNull.Value ? Convert.ToInt32(dr["TemplateId"]) : TemplateId = null;
			SiteContentId = dr["SiteContentId"] != DBNull.Value ? Convert.ToInt32(dr["SiteContentId"]) : SiteContentId = null;
			EventId = dr["EventId"] != DBNull.Value ? Convert.ToInt32(dr["EventId"]) : EventId = null;
			DocumentName = dr["DocumentName"] != DBNull.Value ? Convert.ToString(dr["DocumentName"]) : DocumentName = null;
			DocumentType = dr["DocumentType"] != DBNull.Value ? Convert.ToString(dr["DocumentType"]) : DocumentType = null;
			DocumentSize = dr["DocumentSize"] != DBNull.Value ? Convert.ToInt32(dr["DocumentSize"]) : DocumentSize = null;
			IsDeleted = dr["IsDeleted"] != DBNull.Value ? Convert.ToBoolean(dr["IsDeleted"]) : IsDeleted = null;
			CreateDate = dr["CreateDate"] != DBNull.Value ? Convert.ToDateTime(dr["CreateDate"]) : CreateDate = null;
			LastModifiedDate = dr["LastModifiedDate"] != DBNull.Value ? Convert.ToDateTime(dr["LastModifiedDate"]) : LastModifiedDate = null;
		}

		public static List<FileAttach> MapFrom(DataSet ds)
		{
			// Initialise Collection.
			List<FileAttach> objects = new List<FileAttach>();

			// Validation.
			if (ds == null)
				throw new ApplicationException("Cannot map to dataset null.");
			
			if (ds.Tables[TableName].Rows.Count == 0)
				return objects;

			if (ds.Tables[TableName] == null)
				throw new ApplicationException("Cannot find table [dbo].[FileAttach] in DataSet.");

			if (ds.Tables[TableName].Rows.Count < 1)
				throw new ApplicationException("Table [dbo].[FileAttach] is empty.");

			// Map DataSet to Instance.
			foreach (DataRow dr in ds.Tables[TableName].Rows)
			{
				FileAttach instance = new FileAttach();
				instance.MapFrom(dr);
				objects.Add(instance);
			}

			// Return collection.
			return objects;
		}

		#endregion

		#region CRUD Methods

		public static FileAttach Get(Int32 attachId)
		{
			FileAttach instance = new FileAttach();

			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFileAttach_SELECT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, attachId))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					if (ds == null || ds.Tables[0].Rows.Count == 0) throw new ApplicationException(String.Format("Could not get FileAttach ID:{0} from Database.", attachId));
					ds.Tables[0].TableName = TableName;

					instance.MapFrom(ds.Tables[0].Rows[0]);
				}
			}
			return instance;
		}

		#region INSERT

		private void Insert(Int32? templateId, Int32? siteContentId, Int32? eventId, String documentName, String documentType, Int32? documentSize, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFileAttach_INSERT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, templateId, siteContentId, eventId, documentName, documentType, documentSize, isDeleted, createDate, lastModifiedDate))
			{
				AttachId = Convert.ToInt32(transaction == null ? db.ExecuteScalar(dbCommand) : db.ExecuteScalar(dbCommand, transaction));
			}
		}

		/// <summary>
		/// Insert current FileAttach to database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Insert(DbTransaction transaction = null)
		{
			Insert(TemplateId, SiteContentId, EventId, DocumentName, DocumentType, DocumentSize, IsDeleted, CreateDate, LastModifiedDate, transaction);
		}

		#endregion

		#region UPDATE

		private static void Update(Int32? attachId, Int32? templateId, Int32? siteContentId, Int32? eventId, String documentName, String documentType, Int32? documentSize, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFileAttach_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@attachId"].Value = attachId;
				dbCommand.Parameters["@templateId"].Value = templateId;
				dbCommand.Parameters["@siteContentId"].Value = siteContentId;
				dbCommand.Parameters["@eventId"].Value = eventId;
				dbCommand.Parameters["@documentName"].Value = documentName;
				dbCommand.Parameters["@documentType"].Value = documentType;
				dbCommand.Parameters["@documentSize"].Value = documentSize;
				dbCommand.Parameters["@isDeleted"].Value = isDeleted;
				dbCommand.Parameters["@createDate"].Value = createDate;
				dbCommand.Parameters["@lastModifiedDate"].Value = lastModifiedDate;

				if (transaction == null)
					db.ExecuteNonQuery(dbCommand);
				else
					db.ExecuteNonQuery(dbCommand, transaction);
			}
		}

		private static void Update(Int32? attachId, Int32? templateId, Int32? siteContentId, Int32? eventId, String documentName, String documentType, Int32? documentSize, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate)
		{
			Update(attachId, templateId, siteContentId, eventId, documentName, documentType, documentSize, isDeleted, createDate, lastModifiedDate, null);
		}

		private static void Update(FileAttach fileAttach)
		{
			fileAttach.Update();
		}

		private static void Update(FileAttach fileAttach, DbTransaction transaction)
		{
			fileAttach.Update(transaction);
		}

		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Update(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFileAttach_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@attachId"].SourceColumn = "AttachId";
				dbCommand.Parameters["@templateId"].SourceColumn = "TemplateId";
				dbCommand.Parameters["@siteContentId"].SourceColumn = "SiteContentId";
				dbCommand.Parameters["@eventId"].SourceColumn = "EventId";
				dbCommand.Parameters["@documentName"].SourceColumn = "DocumentName";
				dbCommand.Parameters["@documentType"].SourceColumn = "DocumentType";
				dbCommand.Parameters["@documentSize"].SourceColumn = "DocumentSize";
				dbCommand.Parameters["@isDeleted"].SourceColumn = "IsDeleted";
				dbCommand.Parameters["@createDate"].SourceColumn = "CreateDate";
				dbCommand.Parameters["@lastModifiedDate"].SourceColumn = "LastModifiedDate";

				using (DataSet ds = new DataSet())
				{
					MapTo(ds);
					ds.AcceptChanges();
					ds.Tables[0].Rows[0].SetModified();

					if (transaction == null)
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, UpdateBehavior.Standard);
					else
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, transaction);
				}
			}
		}

		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		private void Update()
		{
			Update((DbTransaction)null);
		}

		#endregion

		#region DELETE
		
		public static void Delete(Int32? attachId, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFileAttach_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, attachId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
		}

		public static void Delete(Int32? attachId)
		{
			Delete(
				attachId);
		}

		/// <summary>
		/// Delete current FileAttach from database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		public void Delete(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFileAttach_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, AttachId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
			AttachId = null;
		}

		/// <summary>
		/// Delete current FileAttach from database.
		/// </summary>
		public void Delete()
		{
			Delete((DbTransaction)null);
		}

		#endregion

		#region SEARCH

		private static List<FileAttach> Search(Int32? attachId = null, Int32? templateId = null, Int32? siteContentId = null, Int32? eventId = null, String documentName = null, String documentType = null, Int32? documentSize = null, Boolean? isDeleted = null, DateTime? createDate = null, DateTime? lastModifiedDate = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFileAttach_SEARCH";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, attachId, templateId, siteContentId, eventId, documentName, documentType, documentSize, isDeleted, createDate, lastModifiedDate))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					ds.Tables[0].TableName = TableName;
					return MapFrom(ds);
				}
			}
		}

		private static List<FileAttach> Search(FileAttach searchObject)
		{
			return Search(searchObject.AttachId, searchObject.TemplateId, searchObject.SiteContentId, searchObject.EventId, searchObject.DocumentName, searchObject.DocumentType, searchObject.DocumentSize, searchObject.IsDeleted, searchObject.CreateDate, searchObject.LastModifiedDate);
		}

		#endregion

		#endregion

		#endregion
	}
}

